package com.zzzy.cont.controller.mycon;

import com.zzzy.common.util.FileUtil;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.common.vo.PageResult;
import com.zzzy.cont.dao.entity.ConAttachment;
import com.zzzy.cont.service.ConAttachmentService;
import com.zzzy.cont.vo.contract.attachment.ConAttachmentAddReq;
import com.zzzy.cont.vo.contract.attachment.ConAttachmentPageReq;
import com.zzzy.cont.vo.pageoffice.OpenOfficeFileRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Tag(name = "我的合同-附件", description = "con105")
@Validated
@RestController
@RequestMapping("/con/myConAttachment")
public class MyConAttachmentController {

    @Resource
    private ConAttachmentService conAttachmentService;

    @Operation(summary = "分页列表")
    @GetMapping(value = "/page")
    @PreAuthorize("@pvs.hasPermission('con:my:page')")
    public AjaxResult<PageResult<ConAttachment>> page(@Valid @ParameterObject ConAttachmentPageReq queryReq) {
        PageResult<ConAttachment> docAttachmentPage = conAttachmentService.getConAttachmentPage(queryReq);
        return AjaxResult.success(docAttachmentPage);
    }

    @PostMapping("/add")
    @Operation(summary = "新增合同附件")
    @Parameter(name = "file", description = "上传的附件")
    @PreAuthorize("@pvs.hasPermission('con:my:page')")
    public AjaxResult add(MultipartFile file, @Valid @ParameterObject ConAttachmentAddReq addReq) {
        conAttachmentService.addConAttachment(addReq);
        return AjaxResult.success();
    }

    @GetMapping(value = "/getPageOfficeFile/{id}")
    @Operation(summary = "打开文件")
    @Parameter(name = "id", description = "合同附件ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:my:page')")
    public AjaxResult<OpenOfficeFileRes> getPageOfficeFile(@NotNull(message = "合同附件ID不能为空") @PathVariable("id") Long id) {
        String openFileUrl = "/con/myConAttachment/getAttachmentFileStream/" + id;
        OpenOfficeFileRes openOfficeFileRes = conAttachmentService.getPageOfficeResByConId(id, openFileUrl);
        return AjaxResult.success(openOfficeFileRes);
    }

    @GetMapping(value = "/getAttachmentFileStream/{id}")
    @Operation(summary = "获取文件流")
    @Parameter(name = "id", description = "合同附件ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:my:page')")
    public void getAttachmentFileStream(@NotNull(message = "合同附件ID不能为空") @PathVariable("id") Long id, HttpServletResponse response) {
        ConAttachment conAttachment = conAttachmentService.getConAttachmentById(id);
        FileUtil.outputFileStream(response, conAttachment.getFileBlob(), conAttachment.getName());
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除合同附件")
    @Parameter(name = "id", description = "合同附件ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:my:page')")
    public AjaxResult delete(@NotNull(message = "合同附件ID不能为空") @PathVariable("id") Long id) {
        conAttachmentService.deleteById(id);
        return AjaxResult.success();
    }
}
